package com.example.hasee.data;

import android.os.AsyncTask;
import android.util.Log;

import com.example.hasee.data.entity.School;
import com.example.hasee.data.entity.Student;
import com.example.hasee.data.entity.Weather;
import com.example.hasee.utils.NetUtils;
import com.example.hasee.weather.WeatherInfoParse;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

/**数据层对外实现类
 * 负责对整个App提供所需要的数据因为需要多界面去访问，为保持数据的唯一使用“单例设计模式”
 * @author 李四
 */
//http://tool.oschina.net/codeformat/json
//http://v.juhe.cn/weather/index?format=2&cityname=%E8%8B%8F%E5%B7%9E&key=您申请的KEY
public class DataStore {
    private static DataStore INSTANCE = new DataStore();
    private DataCallback<Weather> mCallback;
    public ArrayList<String> funcMenuList;
    private DataStore(){
        funcMenuList=new ArrayList<>();
        funcMenuList.add("信息的添加");
        funcMenuList.add("信息的展示");
        funcMenuList.add("用户的设置");
        funcMenuList.add("天气信息");
        funcMenuList.add("用户退出");
    }
    public static DataStore getINSTANCE(){
        return  INSTANCE;
    }
    public ArrayList<String> getFuncMenuList(){
        return funcMenuList;
    }
    /**
     * 判断当前用户是否登入
     * @return
     */
    public Student getCurrentStudent(){
        return BmobUser.getCurrentUser(Student.class);
    }
    /*
    * 用户登陆
    * student 要登陆的用户
    * callback 数据处理的回调接口
    * */
    public void userLogin(Student student, final DataCallback<Student> callback){
        student.login(new SaveListener<Student>() {
            @Override
            public void done(Student student, BmobException e) {
                if(e==null){
                    //登陆成功
                    callback.onSuccess(student);
                }else {
                    //登陆失败
                    callback.onFail("登陆失败，请重新尝试");
                }
            }
        });
    }
    /*
    * 注册
    * */
    public void userSignUp(Student student,final DataCallback<Student> callback){
        student.signUp(new SaveListener<Student>() {
            @Override
            public void done(Student student, BmobException e) {
                if(e==null){
                    //登陆成功
                    callback.onSuccess(student);
                }else {
                    //登陆失败
                    callback.onFail("注册失败，请重新尝试");
                }
            }
        });
    }
    //添加院校信息
    public void addSchool(School school){
        school.save(new SaveListener<String>() {
            @Override
            public void done(String s, BmobException e) {
                Log.e("MyLog",s);
            }
        });
    }
    //获取全部院校信息
    //callback 数据处理回调接口
    public void getSchoolList(final DataCallback<List<School>> callback){
        BmobQuery<School> query=new BmobQuery<>();
        query.findObjects(new FindListener<School>() {
            @Override
            public void done(List<School> list, BmobException e) {
                if (e==null){
                    callback.onSuccess(list);
                }else {
                    callback.onFail("院校信息获取失败");
                }
            }
        });
    }

    /**
     * 更新学生信息
     * @param student
     */
    public void updateUser(Student student){
        student.update(new UpdateListener() {
            @Override
            public void done(BmobException e) {
                Log.e("MyLog","用户信息更新");
            }
        });
    }
    public void getWeatherInfo(DataCallback<Weather> callback){
        mCallback=callback;
        new WeatherTask().execute();
    }
    private class WeatherTask extends AsyncTask<Void,Void,Weather>{
        @Override
        protected Weather doInBackground(Void... voids) {
            try {
                URL url=new URL("http://v.juhe.cn/weather/index?format=2&cityname=%E8%8B%8F%E5%B7%9E&key=696e5152cfc5460e6e8ae2bce0bfc7fe");
                InputStream inputStream=NetUtils.getNetData(url);
                BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream));
                StringBuilder builder=new StringBuilder();
                String line="";
                while ((line=reader.readLine())!=null){
                    builder.append(line);
                }
                String jsonString=builder.toString();
                Weather weather=WeatherInfoParse.parseInfo(jsonString);
                return weather;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
        @Override
        protected void onPostExecute(Weather weather) {
            super.onPostExecute(weather);
            //回传给Presenter
            mCallback.onSuccess(weather);
        }
    }
}
//696e5152cfc5460e6e8ae2bce0bfc7fe